package com.mingyizhudao.app.view.recommend;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.http.DataHandler;
import com.dixiang.framework.widget.LoadingDialog;
//import com.dixiang.framework.network.Net;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.entity.DeptEntity;
import com.mingyizhudao.app.entity.HospitalEntity;
import com.mingyizhudao.app.view.MYBaseActivity;
import com.mingyizhudao.app.view.recommend.adapter.HospitalDeptAdapter;
import com.mingyizhudao.app.view.recommend.bean.HospitalDeptEntityView;
import com.mingyizhudao.app.view.recommend.bean.MenuItem;
import com.mingyizhudao.app.view.recommend.bean.TempDeptEntityView;

public class HospitalToDeptActivity extends MYBaseActivity{
	private ListView lv_hospital_dept;
	private HospitalDeptAdapter hospitalDeptAdapter;
	private String hospitalViewUrl;
	private HospitalEntity  hospitalEntity;
	private ArrayList<MenuItem> menuItems=new ArrayList<MenuItem>();
	private HospitalDeptEntityView hospitalDeptEntityView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_hospital_dept);
		super.onCreate(savedInstanceState);
		initView();
		getHospitalDate();
	}
	
	@Override
	public void initHeaderView() {
		setTopViewBg(R.color.actionbar_bg_color);
		initBackView(R.drawable.left_back_white_icon, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initView(){
		activity = this;
		Intent intent = getIntent();
		hospitalViewUrl = intent.getExtras().getString( "hospitalViewUrl" );
		hospitalEntity = (HospitalEntity) getIntent().getSerializableExtra("hospitalEntity");
		setTitleViewValue( 0, 0, R.color.white );
		setTitleViewValue( hospitalEntity.getName() );
		
		lv_hospital_dept = (ListView) findViewById(R.id.lv_hospital_dept);
	}
	
	
	
	private void getHospitalDate(){
		LoadingDialog.getInstance(activity).show();
		String url = hospitalViewUrl+hospitalEntity.getId() ;//+"&api=4"
		
		QJNetUICallback callback = new QJNetUICallback<QjResult<HospitalDeptEntityView>>(activity) {
			@Override
			public void onSuccess(QjResult<HospitalDeptEntityView> result) {
				hospitalDeptEntityView = result.getResults();
				if( hospitalDeptEntityView!=null )
				{
					getDeptMenus();
				}else {
					Toast.makeText( activity, "获取数据失败！", 0 ).show();
				}
			}

			public void onError(Exception e, QjResult<HospitalDeptEntityView> result) {
				super.onError(e, result);
			};

			public void onCompleted(Exception e, QjResult<HospitalDeptEntityView> result) {
				super.onCompleted(e, result);
			};
		};
		String path = new DataHandler().getDataObject(activity, url,null, new TypeToken<QjResult<HospitalDeptEntityView>>() {}, callback);
	}
	
	private void getDeptMenus(){
		TempDeptEntityView cohospitalDepts = hospitalDeptEntityView.getDepartments();
		
		int mainMenuNum = 0;//一级菜单总个数
		int facultyNum = 0;//二级菜单总个数
		
		ArrayList<MenuItem> tempMenuItems =null;
		
		if(cohospitalDepts.口腔科!=null){
			facultyNum+=cohospitalDepts.口腔科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.口腔科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.口腔科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"口腔科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "kouqiang" );
			menuItems.add(mi);
			
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.外科!=null){
			facultyNum+=cohospitalDepts.外科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.外科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.外科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			
			MenuItem mi=new MenuItem(true,"外科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "waike" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.妇产科!=null){
			facultyNum+=cohospitalDepts.妇产科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.妇产科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.妇产科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			
			MenuItem mi=new MenuItem(true,"妇产科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "fuchan" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.眼科!=null){
			facultyNum+=cohospitalDepts.眼科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.眼科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.眼科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"眼科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "yanke" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.骨科!=null){
			facultyNum+=cohospitalDepts.骨科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.骨科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.骨科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"骨科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "guke" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.小儿外科!=null){
			facultyNum+=cohospitalDepts.小儿外科.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.小儿外科.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.小儿外科.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"小儿外科",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "xiaoer" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		if(cohospitalDepts.其他!=null){
			facultyNum+=cohospitalDepts.其他.size();
			tempMenuItems =new ArrayList<MenuItem>();
			for (int count = 0; count <cohospitalDepts.其他.size(); count++) {
				DeptEntity cohosDeptEntity = cohospitalDepts.其他.get(count);
				tempMenuItems.add(new MenuItem(false,cohosDeptEntity.getName(),cohosDeptEntity.getId(),null));
			}
			MenuItem mi=new MenuItem(true,"其他",mainMenuNum+"",tempMenuItems);
			mi.setIconName( "qita" );
			menuItems.add(mi);
			mainMenuNum+=1;
		}
		
		hospitalDeptAdapter = new HospitalDeptAdapter(hospitalEntity, hospitalDeptEntityView.getDeptUrl(), menuItems, activity );
		lv_hospital_dept.setAdapter( hospitalDeptAdapter );
	}
}
